package com.andy.mall.coupon.service.impl;

import com.andy.common.to.MemberPrice;
import com.andy.common.to.SkuReductionTo;
import com.andy.mall.coupon.entity.MemberPriceEntity;
import com.andy.mall.coupon.entity.SkuLadderEntity;
import com.andy.mall.coupon.service.MemberPriceService;
import com.andy.mall.coupon.service.SkuLadderService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.andy.common.utils.PageUtils;
import com.andy.common.utils.Query;

import com.andy.mall.coupon.dao.SkuFullReductionDao;
import com.andy.mall.coupon.entity.SkuFullReductionEntity;
import com.andy.mall.coupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        //6.4)、sku的优惠、满减等信息 mall_sms => sms_sku_ladder、sms_sku_full_reduction、sms_member_price
        //1、sms_sku_ladder优惠信息
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(reductionTo.getSkuId());
        skuLadderEntity.setFullCount(reductionTo.getFullCount());
        skuLadderEntity.setDiscount(reductionTo.getDiscount());
        skuLadderEntity.setAddOther(reductionTo.getCountStatus());
        if(reductionTo.getFullCount() > 0){
            skuLadderService.save(skuLadderEntity);
        }

        //2、sms_sku_full_reduction 满减信息
        SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(reductionTo,reductionEntity);
        if(reductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(reductionEntity);
        }

        //3、sms_member_price 会员信息
        List<MemberPrice> memberPrice = reductionTo.getMemberPrice();

        List<MemberPriceEntity> memberPriceEntityList = memberPrice.stream().map(mp -> {
            MemberPriceEntity mpe = new MemberPriceEntity();
            mpe.setSkuId(reductionTo.getSkuId());
            mpe.setMemberPrice(mp.getPrice());
            mpe.setMemberLevelId(mp.getId());
            mpe.setMemberLevelName(mp.getName());
            return mpe;
        }).filter(item -> {
            return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
        }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntityList);
    }

}